package com.guigusuqi.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigusuqi.app.entity.WxUser;
import com.guigusuqi.app.utils.UserThreadLocal;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.dto.DeputyQuery;
import com.guigusuqi.hy.entity.DeputyCard;
import com.guigusuqi.hy.mapper.DeputyCardMapper;
import com.guigusuqi.hy.service.DeputyCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 副卡管理 服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-13
 */
@Service
public class DeputyCardServiceImpl extends ServiceImpl<DeputyCardMapper, DeputyCard> implements DeputyCardService
{
    @Resource
    private DeputyCardMapper deputyCardMapper;

    @Override
    public Result addDeputyCard(DeputyCard deputyCard)
    {
        int insert = deputyCardMapper.insert(deputyCard);
        if(insert >= 1)
        {
            return Result.success().message("新增副卡成功！");
        }else
        {
            return Result.fail().message("新增副卡失败！");
        }
    }

    @Override
    public Result updateDeputyCard(DeputyCard deputyCard)
    {
        LambdaQueryWrapper<DeputyCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeputyCard::getId,deputyCard.getId());
        int update = deputyCardMapper.update(deputyCard, queryWrapper);
        if(update >= 1)
        {
            return Result.success().message("修改副卡成功！");
        }else
        {
            return Result.fail().message("修改副卡失败！");
        }

    }

    @Override
    public Result pageDeputyCondition(Long current, Long limit, DeputyQuery deputyQuery)
    {
        Page<DeputyCard> deputyCardPage = new Page<>();
        LambdaQueryWrapper<DeputyCard> queryWrapper = new LambdaQueryWrapper<>();
        if(deputyQuery != null)
        {
            String cardNum = deputyQuery.getCardNum();
            String parentNum = deputyQuery.getParentNum();
            String parentPerson = deputyQuery.getParentPerson();
            String parentMobile = deputyQuery.getParentMobile();
            String parentType = deputyQuery.getParentType();
            String cardType = deputyQuery.getCardType();
            String userName = deputyQuery.getUserName();
            String userMobile = deputyQuery.getUserMobile();
            String status = deputyQuery.getStatus();

            if(StringUtils.isNotEmpty(cardNum))
            {
                queryWrapper.like(DeputyCard::getCardNum,cardNum);
            }

            if(StringUtils.isNotEmpty(parentNum))
            {
                queryWrapper.like(DeputyCard::getParentNum,parentNum);
            }

            if(StringUtils.isNotEmpty(parentPerson))
            {
                queryWrapper.like(DeputyCard::getParentPerson,parentPerson);
            }

            if(StringUtils.isNotEmpty(parentMobile))
            {
                queryWrapper.like(DeputyCard::getParentMobile,parentMobile);
            }

            if(StringUtils.isNotEmpty(parentType))
            {
                queryWrapper.like(DeputyCard::getParentType,parentType);
            }

            if(StringUtils.isNotEmpty(cardType))
            {
                queryWrapper.like(DeputyCard::getCardType,cardType);
            }

            if(StringUtils.isNotEmpty(userName))
            {
                queryWrapper.like(DeputyCard::getUserName,userName);
            }

            if(StringUtils.isNotEmpty(userMobile))
            {
                queryWrapper.like(DeputyCard::getUserMobile,userMobile);
            }

            if(StringUtils.isNotEmpty(status))
            {
                queryWrapper.eq(DeputyCard::getStatus,status);
            }
        }
        deputyCardMapper.selectPage(deputyCardPage,queryWrapper);
        long total = deputyCardPage.getTotal();
        List<DeputyCard> records = deputyCardPage.getRecords();
        return Result.success().data("total",total).data("rows",records);
    }

    @Override
    public Result bindDeputyCardByDeputyCardId(String deputyCardId)
    {
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
//        String token = request.getHeader("Authorization");
//        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_" + token);
        WxUser wxUser = UserThreadLocal.get();

        String openid = wxUser.getOpenid();

        List<String> deputyCardIdList = deputyCardMapper.selectDeputyCardIdByOpenid(openid);
        if(deputyCardIdList.size() >= 1)
        {
            return Result.fail().message("该账户已绑定副卡!");
        }

        if(openid != null)
        {
            int insert = deputyCardMapper.bindDeputyCardByDeputyCardId(openid,deputyCardId);
            if(insert >= 1)
            {
                return Result.success().message("绑定成功!");
            }else
            {
                return Result.fail().message("绑定失败!");
            }
        }else
        {
            return Result.fail().message("无法获取openid，请检查token是否过期！");
        }
    }

    @Override
    public List<DeputyCard> selectDeputyCard(String openid)
    {
        //通过openid查找deputyCardId
        List<String> deputyCardIdList = deputyCardMapper.selectDeputyCardIdByOpenid(openid);

        List<DeputyCard> deputyCardList = new ArrayList<>();
        if(deputyCardIdList.size() == 0)
        {
            return deputyCardList;
        }
        LambdaQueryWrapper<DeputyCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeputyCard::getId,deputyCardIdList.get(0));

        return deputyCardMapper.selectList(queryWrapper);
    }
}
